package com.ficus.road.maintain.third.zh.coverage;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.coverage.UnitCoverageDay;
import com.ficus.road.maintain.core.model.coverage.UnitCoverageDayZH;
import com.ficus.road.maintain.dao.coverage.UnitCoverageDayDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/16 下午2:09
 */
@Component
public class ZHUnitCoverageDay extends BaseZH {

    @Autowired
    private UnitCoverageDayDao unitCoverageDayDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjSectionDayCover";
        errMsg = "调用中海接口获取标段日覆盖率异常！";
        successMsg = "调用中海接口获取标段日覆盖率成功！";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/10 6-22 * * ?", zone = "GMT+8:00")
    public void refreshUnitCoverageDay() {
        Date today = new Date();
        String todayStr = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        arg1 = "{\"datatime\":\"" + todayStr + "\"}";
        String zhData = null;
        int i = 0;
        while (i < 10) {
            zhData = getZHData();
            if (ObjectUtils.isNotEmpty(zhData)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(zhData)) {
            return;
        }
        List<UnitCoverageDayZH> unitCoverageDayZHList = JSONArray.parseArray(zhData, UnitCoverageDayZH.class);
        List<UnitCoverageDay> unitCoverageDayList = unitCoverageDayZHList.stream().map(item -> item.convert2UnitCoverageDay(item, today, todayStr)).collect(Collectors.toList());
        unitCoverageDayList.forEach(unitCoverageDay -> {
            unitCoverageDayDao.saveOrUpdate(unitCoverageDay);
        });
    }
}
